#include "CEGUIInfor.h"
#include <ogre\Ogre.h>
#include "KnowledgeCenter.h"
std::string& decode(const CEGUI::String& str);
Branch1::CEGUIInforManager::CEGUIInforManager():_wmgr(CEGUI::WindowManager::getSingleton()),_timeLine(0.0f)
{
	CEGUI::SchemeManager::getSingleton().create("TaharezLook.scheme");
}
Branch1::CEGUIInforManager::~CEGUIInforManager(){}
bool Branch1::CEGUIInforManager::init()
{
	CEGUI::Window *sheet = _wmgr.createWindow("DefaultWindow", "CEGUIDemo/Sheet");
	CEGUI::Window *info = _wmgr.createWindow("TaharezLook/Button", "Info");

	info->setText("Info");
	
	info->setSize(CEGUI::UVector2(CEGUI::UDim(0.80, 200), CEGUI::UDim(0.1, 10)));

	CEGUI::Window* st = _wmgr.createWindow("TaharezLook/StaticText", "textPanel");
	st->setSize(CEGUI::UVector2(cegui_reldim(0.96f), cegui_reldim( 0.25f)));
	st->setProperty("FrameEnabled", "false");
	st->setProperty("BackgroundEnabled", "false");


	sheet->addChildWindow(st);
	//sheet->addChildWindow(info);




	//sheet->addChildWindow(info);
	CEGUI::System::getSingleton().setGUISheet(sheet);
	return true;
}
void Branch1::CEGUIInforManager::addTime(float interval)
{
	
	CEGUI::Window * info = _wmgr.getWindow("Info");
	Ogre::Vector3 camPosit = DAISY::VariantData<Ogre::Vector3>::get(Branch1::KnowledgeCenter::getInstance().getOffice("PlayerInfo")->getData("MainCameraPosition"));
	Ogre::Quaternion camQuter =  DAISY::VariantData<Ogre::Quaternion>::get(Branch1::KnowledgeCenter::getInstance().getOffice("PlayerInfo")->getData("MainCameraOrientation"));
	Ogre::Vector3 playerPosit = DAISY::VariantData<Ogre::Vector3>::get(Branch1::KnowledgeCenter::getInstance().getOffice("PlayerInfo")->getData("PlayerPosition"));


	info->setText(/*"timeline:"+Ogre::StringConverter::toString(_timeLine)+*/"cp: "+Ogre::StringConverter::toString(camPosit)+" co:"+Ogre::StringConverter::toString(camQuter)+"pp:"+Ogre::StringConverter::toString(playerPosit));
}
void Branch1::CEGUIInforManager::injectKeyDown(const DAISY::KeyEvent& evt)
{
	if(evt.getKey() == KC_M)
	{
		Ogre::Vector3 camPosit = DAISY::VariantData<Ogre::Vector3>::get(Branch1::KnowledgeCenter::getInstance().getOffice("PlayerInfo")->getData("MainCameraPosition"));
		Ogre::Quaternion camQuter =  DAISY::VariantData<Ogre::Quaternion>::get(Branch1::KnowledgeCenter::getInstance().getOffice("PlayerInfo")->getData("MainCameraOrientation"));
		Ogre::Vector3 playerPosit = DAISY::VariantData<Ogre::Vector3>::get(Branch1::KnowledgeCenter::getInstance().getOffice("PlayerInfo")->getData("PlayerPosition"));
		Ogre::Quaternion playerQuter = DAISY::VariantData<Ogre::Quaternion>::get(Branch1::KnowledgeCenter::getInstance().getOffice("PlayerInfo")->getData("PlayerOrientation"));


		Ogre::LogManager::getSingleton().logMessage("##CUTSCENE##");
		Ogre::LogManager::getSingleton().logMessage(
			"CameraPx=\""+Ogre::StringConverter::toString(camPosit.x)+
			"\" CameraPy=\""+Ogre::StringConverter::toString(camPosit.y)+
			"\" CameraPz=\""+Ogre::StringConverter::toString(camPosit.z)+
			"\" CameraQw=\""+Ogre::StringConverter::toString(camQuter.w)+
			"\" CameraQx=\""+Ogre::StringConverter::toString(camQuter.x)+
			"\" CameraQy=\""+Ogre::StringConverter::toString(camQuter.y)+
			"\" CameraQz=\""+Ogre::StringConverter::toString(camQuter.z)+
			"\" PlayerPx=\""+Ogre::StringConverter::toString(playerPosit.x)+
			"\" PlayerPy=\""+Ogre::StringConverter::toString(playerPosit.y)+
			"\" PlayerPz=\""+Ogre::StringConverter::toString(playerPosit.z)+
			"\" PlayerQw=\""+Ogre::StringConverter::toString(playerQuter.w)+
			"\" PlayerQx=\""+Ogre::StringConverter::toString(playerQuter.x)+
			"\" PlayerQy=\""+Ogre::StringConverter::toString(playerQuter.y)+
			"\" PlayerQz=\""+Ogre::StringConverter::toString(playerQuter.z)
			);
	}
}
void Branch1::CEGUIInforManager::injectKeyUp(const DAISY::KeyEvent& evt)
{

}
void Branch1::CEGUIInforManager::shutDown()
{
}
std::string& decode(const CEGUI::String& str)
{
	char sdes[2048];
	static std::string result;
	int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
	wchar_t* wcstr = new wchar_t[len];
	MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, wcstr, len);

	int wlen = WideCharToMultiByte(CP_ACP, 0, wcstr, -1, NULL, 0, NULL, NULL);
	char* nstr = new char[wlen];
	WideCharToMultiByte(CP_ACP, 0, wcstr, -1, nstr, wlen, NULL, NULL);

	strcpy(sdes, nstr);

	delete[] wcstr;
	delete[] nstr;

	result = sdes;

	return result;
}